package com.briup.pts.web.controller;

import com.briup.pts.config.response.Result;
import com.briup.pts.domain.bean.FenceHouse;
import com.briup.pts.domain.vo.FenceHouseVO;
import com.briup.pts.service.FenceHouseService;
import com.github.pagehelper.PageInfo;
import jakarta.validation.Valid;
import jakarta.validation.constraints.Min;
import lombok.RequiredArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Validated
@RestController
@RequestMapping("/fenceHouse")
@RequiredArgsConstructor
public class FenceHouseController {

    private final FenceHouseService service;

    @GetMapping("/query")
    public Result<?> query(
         @Min(value = 1,message = "当前页面至少要不小于1") Integer pageNum,
         @Min(value = 1,message = "页面大小至少要不小于1") Integer pageSize,
                           String fhName){
        // 调用service层方法，分页查询数据
        PageInfo<FenceHouse> pageInfo =
                service.pageQuery(pageNum, pageSize, fhName);
        return Result.success(pageInfo);
    }

    @GetMapping("/selectById")
    public Result<?> queryById(String fhId){
        FenceHouseVO vo = service.queryById(fhId);
        return Result.success(vo);
    }

    // @RequestBody注解表示后续的数据需要从请求体中获取，尤其指如json、xml等格式传输的数据
    @PostMapping("/saveOrUpdate")
    public Result<?> saveOrUpdate(@Valid @RequestBody FenceHouse fenceHouse){
        service.saveOrUpdate(fenceHouse);
        return Result.success();
    }

    // @PathVariable注解表示需要从请求路径中获取请求路径的某一段作为参数的值
    @DeleteMapping("/deleteById/{fhId}")
    public Result<?> deleteById(@PathVariable String fhId) {
        service.deleteBatch(List.of(fhId));
        return Result.success();
    }

    @DeleteMapping("/deleteByIdAll")
    public Result<?> deleteByIdAll(@RequestBody List<String> ids) {
        service.deleteBatch(ids);
        return Result.success();
    }
}
